package com.ds;

import com.ds.cluster.ServerNode;
import com.ds.common.JDSConstants;
import com.ds.common.JDSException;
import com.ds.common.logging.Log;
import com.ds.common.logging.LogFactory;
import com.ds.esb.util.EsbFactory;
import com.ds.listener.CheckOnLineTask;
import com.ds.server.JDSServer;
import com.ds.server.SubSystem;
import com.ds.server.eumus.SystemType;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class JDSInit extends HttpServlet {
    private static final Log logger = LogFactory.getLog(
            JDSConstants.CONFIG_KEY, JDSInit.class);

    private static final long serialVersionUID = -7181186633660301865L;

    @Override
    public void init() throws ServletException {

        try {
            JDSServer.getInstance();
            EsbFactory.initBus();


            List<ServerNode> systems = JDSServer.getClusterClient().getAllServer();

            for (ServerNode node : systems) {
                SubSystem system = JDSServer.getClusterClient().getSystem(node.getId());
                if (system != null) {
                    if (system.getType().equals(SystemType.factory)) {
                        logger.info("start CheckOnLineTask Thread facory=" + system.getEnname());
                        Executors.newScheduledThreadPool(10).scheduleAtFixedRate(new CheckOnLineTask(system.getEnname()), 2, 3, TimeUnit.MINUTES);
                    }
                }

            }


//			ClusterClient client=JDSServer.getClusterClient();
//			client.login();

        } catch (final JDSException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}
